package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.CanCheckPositionService;
import com.iot08.admin.service.ChannelAreaService;
import com.iot08.admin.service.ChannelService;
import com.iot08.admin.service.RoleService;
import com.iot08.common.config.redis.RedisService;
import com.iot08.common.entity.admin.Channel;
import com.iot08.common.entity.admin.ChannelArea;
import com.iot08.common.entity.admin.SysRole;
import com.iot08.common.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/10/31 16:41
 * @author: Rock
 */
@Slf4j
@Controller
@RequestMapping("api")
public class ApiController extends BaseController {

    @Resource
    private RedisService redisService;
    @Resource
    private ChannelService channelService;
    @Resource
    private ChannelAreaService channelAreaService;
    @Resource
    private CanCheckPositionService canCheckPositionService;
    @Resource
    private RoleService roleService;
    @Resource(name = "appMongoTemplate")
    protected MongoTemplate mongoTemplate;

    /**
     * @param qd
     * @time: 2020/10/31 16:46
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("code")
    public Response<Object> code(String qd) {
        Map<String, String> map = new HashMap<>();
        map.put("text", qd);
//        log.info("log={}", redisService.exists(qd));
        log.info("log={}", qd);
        return Response.successResponse(map);
    }

    /**
     * 检查添加一级渠道区域
     *
     * @time: 2020/12/7 15:16
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkChannelArea")
    public Response<Object> checkChannelArea() {
        List<Channel> channelList = channelService.find(new Query(Criteria.where("deleted").is(0)));
        for (Channel channel : channelList) {
            ChannelArea channelArea = channelAreaService.findOne(new Query(Criteria.where("channel_id").is(channel.getId())));
            if (channelArea != null) {
                continue;
            }
            channelArea = new ChannelArea();
            channelArea.setName(channel.getName());
            channelArea.setChannelCode(channel.getChannelCode());
            channelArea.setLevel(1);
            channelArea.setId(channelAreaService.createId(1, null));
            channelArea.setDeleted(0);
            channelArea.setCreateTime(new Date());
            channelAreaService.save(channelArea);
        }

        List<SysRole> roles = roleService.find(new Query(Criteria.where("deleted").is(0)));
        Update update = new Update();
        update.set("level", 0);
        for (SysRole role : roles) {
            roleService.updateById(role.getId(), update);
        }
        return Response.successResponse();
    }
}
